<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;
use app\admin\model\store\Shop;
use app\admin\model\service\Service;
use app\admin\model\staff\Staff;
use app\admin\model\wanlshop\Order;
use think\Session;

/**
 * 运营概况
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Datadashboard extends Backend
{
    protected $shop_id = null;
    protected $user_model = null;
    protected $shop_model = null;
    protected $order_model = null;
    protected $service_model = null;
    protected $order_goods_model = null;
    protected $order_item_model = null;
    protected $refund_model = null;
    
    public function _initialize()
    {
        parent::_initialize();
        $this->shop_id = Session::get('shop');
        $this->user_model = new \app\admin\model\User;
        $this->shop_model = new \app\admin\model\store\Shop;
        $this->service_model = new \app\admin\model\service\Service;
        $this->order_model = new \app\admin\model\order\Order;
        $this->order_goods_model = new \app\admin\model\order\Goods;
        $this->order_item_model = new \app\admin\model\order\Item;
        $this->refund_model = new \app\admin\model\wanlshop\Refund;
    }
    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $this->request->filter(['strip_tags']);
        $starttime = strtotime($this->request->request('starttime'));
        $endtime = strtotime($this->request->request('endtime'));

        $column = [];
        if (!$starttime) {
            $starttime = Date::unixtime('day', 0);
            $endtime = Date::unixtime('day', 0, 'end');
        }

        $titleArr = ['实际收款金额','营收统计金额','客户消耗金额','充值卡销售金额','次卡销售金额','单项服务收入','门店总数','服务商总数','会员总数','新增会员数','产品销售金额','退款金额','客单价','开卡充值客户数','成交客户数','订货金额','订货门店数'];
        $list = array_map(function($index, $title) {
            return [
                'title' => $title,
                'value' => $index+1
            ];
        }, array_keys($titleArr), $titleArr);

        foreach ($list as $key => &$val) {
            if ($key == 0) {
                $conditions = [];
                $conditions['o.data_type'] = ['in',['cards','goods']];
                $conditions['o.state'] = 6;
                $conditions['o.status'] = 'normal';
                $conditions['p.pay_way_id'] = ['in',[10,12]];
                $conditions['o.createtime'] = ['between time',[$starttime, $endtime]];
                $val['value'] = $this->order_model->alias('o')
                    ->join('wanlshop_pay_out_trade p','p.order_id = o.id','left')
                    ->where($conditions)
                    ->sum('o.actual_price');

            }elseif ($key == 1) {
                $conditions = [];
                $conditions['o.data_type'] = ['in',['cards','goods']];
                $conditions['o.state'] = 6;
                $conditions['o.status'] = 'normal';
                $conditions['p.pay_way_id'] = ['<>',9];
                $conditions['o.createtime'] = ['between time',[$starttime, $endtime]];
                $val['value'] = $this->order_model->alias('o')
                    ->join('wanlshop_pay_out_trade p','p.order_id = o.id','left')
                    ->where($conditions)
                    ->sum('o.actual_price');
            }elseif ($key == 2) {
                $conditions = [];
                $conditions['o.data_type'] = ['in',['cards','goods']];
                $conditions['o.state'] = 6;
                $conditions['o.status'] = 'normal';
                $conditions['o.createtime'] = ['between time',[$starttime, $endtime]];
                $val['value'] = $this->order_model->alias('o')
                    ->join('wanlshop_order_item_pay p', 'o.id=p.order_id', 'left')
                    ->where($conditions)
                    ->sum('p.money');
            }elseif ($key == 3) {
                $conditions = [];
                $conditions['o.data_type'] = 'cards';
                $conditions['o.state'] = 6;
                $conditions['o.status'] = 'normal';
                $conditions['o.createtime'] = ['between time',[$starttime, $endtime]];
                $conditions['p.pay_way_id'] = ['<>',9];
                $conditions['i.type'] = '4';
                $val['value'] = $this->order_item_model->alias('i')
                    ->join('wanlshop_order o', 'o.id=i.order_id', 'left')
                    ->join('wanlshop_pay_out_trade p', 'o.id=p.order_id', 'left')
                    ->where($conditions)
                    ->sum('p.price');
            }elseif ($key == 4) {
                $conditions = [];
                $conditions['o.data_type'] = 'cards';
                $conditions['o.state'] = 6;
                $conditions['o.status'] = 'normal';
                $conditions['o.createtime'] = ['between time',[$starttime, $endtime]];
                $conditions['p.pay_way_id'] = ['<>',9];
                $conditions['i.type'] = ['<>','4'];
                $val['value'] = $this->order_item_model->alias('i')
                    ->join('wanlshop_order o', 'o.id=i.order_id', 'left')
                    ->join('wanlshop_pay_out_trade p', 'o.id=p.order_id', 'left')
                    ->where($conditions)
                    ->sum('p.price');
            }elseif ($key == 5) {
                $conditions = [];
                $conditions['o.data_type'] = 'goods';
                $conditions['o.state'] = 6;
                $conditions['o.status'] = 'normal';
                $conditions['o.createtime'] = ['between time',[$starttime, $endtime]];
                $conditions['p.pay_way_id'] = ['<>',9];
                $conditions['i.gdtype'] = '1';
                $value =  $this->order_goods_model->alias('i')
                                ->join('wanlshop_order o', 'o.id=i.order_id', 'left')
                                ->join('wanlshop_pay_out_trade p', 'o.id=p.order_id', 'left')
                                ->where($conditions)
                                ->field('sum(i.number * i.actual_payment ) as sum')
                                ->select();
                $val['value'] = $value['0']['sum'] ? $value['0']['sum'] : 0;
            }elseif ($key == 6) {
                $conditions = [];
                $conditions['status'] = 'normal';
                $val['value'] = $this->shop_model->where($conditions)->count();
            }elseif ($key == 7) {
                $conditions = [];
                $conditions['status'] = 'normal';
                $val['value'] = $this->service_model->where($conditions)->count();
            }elseif ($key == 8) {
                $conditions = [];
                $conditions['status'] = 'normal';
                $val['value'] = $this->user_model->where($conditions)->count();
            }elseif ($key == 9) {
                $conditions = [];
                $conditions['status'] = 'normal';
                $conditions['jointime'] = ['between time',[$starttime, $endtime]];
                $val['value'] = $this->user_model->where($conditions)->count();
            }elseif ($key == 10) {
                $conditions = [];
                $conditions['o.data_type'] = 'goods';
                $conditions['o.state'] = 6;
                $conditions['o.status'] = 'normal';
                $conditions['o.createtime'] = ['between time',[$starttime, $endtime]];
                $conditions['p.pay_way_id'] = ['<>',9];
                $conditions['i.gdtype'] = '0';
                $value = $this->order_goods_model->alias('i')
                                ->join('wanlshop_order o', 'o.id=i.order_id', 'left')
                                ->join('wanlshop_pay_out_trade p', 'o.id=p.order_id', 'left')
                                ->where($conditions)
                                ->field('sum(i.number * i.actual_payment ) as sum')
                                ->select();
                $val['value'] = $value['0']['sum'] ? $value['0']['sum'] : 0;
            }elseif ($key == 11) {
                $conditions = [];
                $conditions['state'] = '4';
                $conditions['status'] = 'normal';
                $conditions['completetime'] = ['between time',[$starttime, $endtime]];
                $val['value'] = $this->refund_model->where($conditions)->sum('price');
            }elseif ($key == 12) {
                $conditions = [];
                $conditions['o.data_type'] = ['in',['cards','goods']];
                $conditions['o.state'] = 6;
                // $conditions['o.status'] = 'normal';
                $conditions['o.createtime'] = ['between time',[$starttime, $endtime]];
                $conditions['p.pay_way_id'] = ['<>','9'];

                //当日卡项订单笔数+商品订单笔数+服务订单笔数
                $total_real_count = $this->order_model->alias('o')
                        ->join('wanlshop_pay_out_trade p', 'o.id=p.order_id', 'left')
                        ->where($conditions)
                        ->count();
                //当日卡项（次卡+充值卡）实收金额+商品实收金额+服务订单实收金额
                $total_real_amount = $this->order_model->alias('o')
                        ->join('wanlshop_pay_out_trade p', 'o.id=p.order_id', 'left')
                        ->where($conditions)
                        ->sum('p.price');
                $user_amount = 0;
                if ($total_real_count != 0) {
                    $user_amount = number_format($total_real_amount / $total_real_count, 2);
                }
                $val['value'] = $user_amount;
            }elseif ($key == 13) {
                $conditions = [];
                $conditions['data_type'] = 'cards';
                $conditions['state'] = 6;
                $conditions['status'] = 'normal';
                $conditions['createtime'] = ['between time',[$starttime, $endtime]];
                $val['value'] = $this->order_model->where($conditions)->count();
            }elseif ($key == 14) {
                $conditions = [];
                $conditions['state'] = ['in',['2','3','4','5','6']];
                $conditions['status'] = 'normal';
                $conditions['createtime'] = ['between time',[$starttime, $endtime]];
                $val['value'] = $this->order_model->where($conditions)->count('distinct user_id');
            }elseif ($key == 15) {
                $conditions = [];
                $conditions['data_type'] = 'merchant';
                $conditions['state'] = ['in',['2','3','4','6']];
                $conditions['status'] = 'normal';
                $conditions['b_shop_id'] = ['<>',0];
                $conditions['createtime'] = ['between time',[$starttime, $endtime]];
                $val['value'] = $this->order_model->where($conditions)->sum('actual_price');
            }elseif ($key == 16) {
                $conditions = [];
                $conditions['data_type'] = 'merchant';
                $conditions['state'] = ['in',['2','3','4','6']];
                $conditions['status'] = 'normal';
                $conditions['b_shop_id'] = ['<>',0];
                $conditions['createtime'] = ['between time',[$starttime, $endtime]];
                $val['value'] = $this->order_model->where($conditions)->count();
            }
        }

        $this->assignconfig('starttime', $starttime);
        $this->assignconfig('endtime', $endtime);
        $this->assign('starttime', $starttime);
        $this->assign('endtime', $endtime);
        $this->assign('list', $list);
        return $this->view->fetch();
    }

}
